package com.bbzn.device.client.utils.app.push;


import com.bbzn.device.client.config.UmengProperties;
import com.bbzn.device.client.utils.app.push.android.AndroidUnicast;
import com.bbzn.device.client.utils.app.push.ios.IOSUnicast;

/**
 * @Author: wulongbo
 * @Date : 2021/3/22 18:04
 * @Version 1.0
 */
public class UmengUtil {

    public final static String appkey = UmengProperties.APP_KEY;
    public final static String appMasterSecret = UmengProperties.APP_MASTER_SECRET;

    public static PushClient client = new PushClient();
    ;
    public static AndroidUnicast androidUnicast;

    public static PushClient getClient() {
        if (client == null) {
            synchronized (PushClient.class) {
                if (client == null) {
                    client = new PushClient();
                }
            }
        }
        return client;
    }

    public static AndroidUnicast getUnicast(String appkey, String appMasterSecret) throws Exception {
        if (androidUnicast == null) {
            synchronized (AndroidUnicast.class) {
                if (androidUnicast == null) {
                    androidUnicast = new AndroidUnicast(appkey, appMasterSecret);
                }
            }
        }  //
        return androidUnicast;
    }

    public static void sendAndroidBroadcast(String token, String postData, String title) throws Exception {
        AndroidUnicast unicast = UmengUtil.getUnicast(UmengUtil.appkey, UmengUtil.appMasterSecret);
        // TODO Set your device token
        unicast.setDeviceToken(token);
        unicast.setTicker(postData);
        unicast.setTitle(title);
        unicast.setText(postData);
        unicast.goAppAfterOpen();
        unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        // TODO Set 'production_mode' to 'false' if it's a test device.
        // For how to register a test device, please see the developer doc.
        unicast.setProductionMode();
        // Set customized fields
        // 额外字段放这里
        unicast.setExtraField("message", postData);
        UmengUtil.getClient().send(unicast);
    }

    public static void sendIOSUnicast(String pushToken, String data) throws Exception {
        IOSUnicast unicast = new IOSUnicast(UmengProperties.IOS_KEY, UmengProperties.IOS_SECRET);
        // TODO Set your device token
        unicast.setDeviceToken(pushToken);
        unicast.setAlert(data);
        unicast.setBadge(0);
        unicast.setSound("default");
        // TODO set 'production_mode' to 'true' if your app is under production mode
        unicast.setProductionMode(true);
        //   unicast.setProductionMode(true);
        // Set customized fields
        unicast.setCustomizedField("zzz", "cccc");
        unicast.setTestMode();
//        unicast.setCustomizedField("message", custom.toString());
        System.out.println("==iosDeviceToken:" + unicast.getPostBody());
        client.send(unicast);
    }

    public static void sendMsg(String pushToken, String data) throws Exception {
        if (pushToken.length() < 50) {
            sendAndroidBroadcast(pushToken, data, "把把智能");
        } else {
            UmengUtil.sendIOSUnicast(pushToken, data);
        }

    }

//    public static void main(String[] args) throws Exception {
//        UmengUtil umengUtil = new UmengUtil();
//        umengUtil.sendIOSUnicast("59270cb646a97f53d59c22ca1ac556f9f6f058b01e743d5436bb01f17403c4b1", "测试");
//    }
}
